import list from "../../../components/list.vue"
export default {
    name: "app",
    data() {
        return {
            // 查询
            inquire: {
                gid: '',
                goodsName: '',
                size: '',
                minPrice: '',
                maxPrice: '',
            },
            sizes: [
                { id: 'S', label: 'S' },
                { id: 'M', label: 'M' },
                { id: 'L', label: 'L' },
                { id: 'XL', label: 'XL' }
            ],
            // 分页数据
            page: {
                pageSize: 6,
                total: 12,
            },
            // 新增货物窗口
            addGoodsDialog: false,
            // 新增的货物信息
            addGoods: {
                goodsName: '',
                color: '',
                size: '',
                price: '',
            },
        }
    },
    components: {
        list,
    },
    created() {
        this.showList = []
        this.total = 0
        this.$store.commit('edit_showList', this.showList)
        // this.$store.dispatch('get_current')

        this.search_goods()
    },
    methods: {
        async search_goods() {
            if (this.inquire.minPrice > this.inquire.maxPrice) {
                this.$store.commit('error', '价格区间不合法')
            }
            else {
                const inquire = this.$qs.parse(this.inquire)
                const { data: res } = await this.$http.post('/gim/api/admin/manageGoods/selectGoods', inquire);
                // console.log(res)
                // 获取成功
                if (res.code == 1) {
                    this.tableColumnList = res.response.tableColumnList
                    this.showList = res.response.goodsSelectList
                    this.page.total = res.response.totalNum
                    // 编辑表头
                    this.$store.commit('edit_tableColumnList', this.tableColumnList)
                    // 编辑表格数据
                    this.$store.commit('edit_showList', this.showList)
                    // 编辑分页数据
                    this.$store.commit('edit_page', this.page)
                    // list中改变按钮状态
                    this.$store.commit('edit_isModify', this.$store.state.current.isGoodsManage)
                    this.$store.commit('edit_isDelete', this.$store.state.current.isGoodsManage)
                }
                else if (res.code == 502)
                    this.$store.commit('error', res.msg)
                else {
                    this.showList = []
                    this.total = 0
                    this.$store.commit('edit_showList', this.showList)
                    // 获取失败，调用错误message提示函数
                    this.$store.commit('error', res.response)
                }
            }
        },
        // 新增货物
        async add_goods() {
            if (this.addGoods.goodsName == '' || this.addGoods.color == '' || this.addGoods.size == '' || this.addGoods.price == '') {
                this.$store.commit('error', '请完善货物信息！')
            }
            else {
                const addGoods = this.$qs.parse(this.addGoods)
                const { data: res } = await this.$http.post('/gim/api/admin/manageGoods/insertGoods', addGoods);
                if (res.code == 1) {
                    this.$store.commit('success', res.response)
                    this.search_goods()
                }
                else
                    this.$store.commit('error', res.response)
                this.addGoods = {
                    goodsName: '',
                    color: '',
                    size: '',
                    price: '',
                }
            }
            // 重置
            this.addGoods = {
                goodsName: '',
                color: '',
                size: '',
                price: '',
            }
            this.addGoodsDialog = false
        },

        async modify_goods() {
            const goodsId = this.$store.state.Modify.row.goodsId

            const modifyGoods = {
                goodsName: this.$store.state.Modify.row.goodsName,
                color: this.$store.state.Modify.row.color,
                size: this.$store.state.Modify.row.size,
                price: this.$store.state.Modify.row.price,
            }

            const { data: res } = await this.$http.post('/gim/api/admin/manageGoods/updateGoods/' + goodsId, modifyGoods);
            console.log(res)
            if (res.code == 1)
                this.$store.commit('success', res.response)
            else
                this.$store.commit('error', res.response)
            this.$store.state.Modify.goodsModify = false
        },

        // 删除货物
        async delete_goods() {
            const goodsId = this.$store.state.Delete.row.goodsId
            const { data: res } = await this.$http.get('/gim/api/admin/manageGoods/deleteGoods/' + goodsId);
            if (res.code == 1) {
                this.$store.commit('success', res.response)
                this.search_goods()
            }
            else
                this.$store.commit('error', res.response)
            this.$store.state.Delete.goodsDelete = false
        },
    },
};